package com.kudian.restful.controller;

import com.kudian.common.exception.ApplicationException;
import com.kudian.common.persistence.ResultObj;
import com.kudian.common.utils.JsonUtils;
import com.kudian.common.utils.StringUtils;
import com.kudian.restful.entity.LoginSession;
import com.kudian.restful.service.LoginSessionService;
import com.kudian.restful.service.OrderService;
import com.kudian.restful.vo.order.*;
import com.wordnik.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by Administrator on 2016/4/27.
 */
@RestController
@RequestMapping("order")
public class OrderInfoController {

    private static final Logger logger = Logger.getLogger(OrderInfoController.class);

    @Autowired
    private OrderService orderService;

    @Autowired
    private LoginSessionService loginSessionService;

    @RequestMapping(value = "add", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "创建订单信息", httpMethod = "POST", notes = "创建订单信息", response = ResultObj.class)
    public AddOrderRetVO add(@RequestBody AddOrderVO vo) {
        AddOrderRetVO o = new AddOrderRetVO();
        if (vo == null || StringUtils.isBlank(vo.getAccess_token())) {
            o.setErrcode(9999);
            o.setErrmsg("创建订单信息失败!access_token为空");
            return o;
        }
        try {
            LoginSession ent = loginSessionService.getInfo(vo.getAccess_token());
            if (ent == null) {
                o.setErrcode(9999);
                o.setErrmsg("access_token已失效!");
                return o;
            }
            o.setErrcode(0);
            o.setErrmsg("创建订单信息成功!");
            o.setOrderIds(orderService.add(vo));
        } catch (ApplicationException e) {
            logger.error(e);
            o.setErrcode(e.getErrcode());
            o.setErrmsg(e.getErrmsg());
        } catch (Exception e) {
            logger.error(e);
            o.setErrcode(9999);
            o.setErrmsg("创建订单信息失败!");
        }
        return o;
    }

    @RequestMapping(value = "submit", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "提交订单", httpMethod = "POST", notes = "提交订单,修改的是收件人信息，优惠和产品不能不动", response = ResultObj.class)
    public ResultObj submit(@RequestBody SubmitOrderVO vo) {
        logger.info("submit=>" + JsonUtils.toJson(vo));
        ResultObj o = new ResultObj();
        if (vo == null || StringUtils.isBlank(vo.getAccess_token())) {
            o.setErrcode(9999);
            o.setErrmsg("提交订单信息失败!access_token为空");
            return o;
        }

        try {
            LoginSession ent = loginSessionService.getInfo(vo.getAccess_token());
            if (ent == null) {
                o.setErrcode(9999);
                o.setErrmsg("access_token已失效!");
                return o;
            }

            o.setErrcode(0);
            o.setErrmsg("提交订单信息成功!");
            orderService.submit(vo);
        } catch (Exception e) {
            logger.error(e);
            o.setErrcode(9999);
            o.setErrmsg("提交订单信息失败!");
        }
        return o;
    }

    @RequestMapping(value = "details", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询自己某些订单明细", httpMethod = "POST", notes = "查询自己某些订单明细，返回优惠信息，且已计算完毕", response = ResultObj.class)
    public @ResponseBody DetailOrderRetVO details(@RequestBody DetailOrderVO vo) {
        logger.info("details=>" + JsonUtils.toJson(vo));
        DetailOrderRetVO o = new DetailOrderRetVO();
        if (vo == null || StringUtils.isBlank(vo.getAccess_token())) {
            o.setErrcode(9999);
            o.setErrmsg("查询自己某一订单明细失败!access_token为空");
            return o;
        }

        try {
            LoginSession ent = loginSessionService.getInfo(vo.getAccess_token());
            if (ent == null) {
                o.setErrcode(9999);
                o.setErrmsg("查询自己某一订单明细access_token已失效!");
                return o;
            }

            o.setErrcode(0);
            o.setErrmsg("查询自己某一订单明细信息成功!");
            o.setInfos(orderService.details(vo));
        } catch (Exception e) {
            logger.error(e);
            o.setErrcode(9999);
            o.setErrmsg("查询自己某一订单明细信息失败!");
        }
        return o;
    }

    @RequestMapping(value = "pay", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "直接支付订单", httpMethod = "POST", notes = "直接支付订单", response = ResultObj.class)
    public @ResponseBody ResultObj pay(@RequestBody AddOrderVO vo) {
        ResultObj o = new ResultObj();

        return o;
    }

    @RequestMapping(value = "querybyself", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询客户自己订单信息", httpMethod = "POST", notes = "查询客户自己订单信息", response = QueryOrderInfoRetVO.class)
    public @ResponseBody QueryOrderInfoRetVO querybyself(@RequestBody QueryOrderVO vo) {
        logger.info("querybyself=>" + JsonUtils.toJson(vo));
        QueryOrderInfoRetVO o = new QueryOrderInfoRetVO();

        if (vo == null || StringUtils.isBlank(vo.getAccess_token())) {
            o.setErrcode(9999);
            o.setErrmsg("查询订单信息失败!access_token为空");
            return o;
        }

        try {
            LoginSession ent = loginSessionService.getInfo(vo.getAccess_token());
            if (ent == null) {
                o.setErrcode(9999);
                o.setErrmsg("access_token已失效!");
                return o;
            }

            o = orderService.querybyself(vo);
            o.setErrcode(0);
            o.setErrmsg("查询订单信息成功!");
        } catch (Exception e) {
            logger.error(e);
            o.setErrcode(9999);
            o.setErrmsg("查询订单信息失败!");
        }
        return o;
    }

    @RequestMapping(value = "receivingConfirmationBySelf", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "客户确认收货", httpMethod = "POST", notes = "客户确认收货", response = ResultObj.class)
    public @ResponseBody ResultObj receivingConfirmationBySelf(@RequestBody ReceivingOrderVO vo) {
        logger.info("receivingConfirmationBySelf=>" + JsonUtils.toJson(vo));
        ResultObj o = new ResultObj();

        if (vo == null || StringUtils.isBlank(vo.getAccess_token())) {
            o.setErrcode(9999);
            o.setErrmsg("客户确认收货失败!access_token为空");
            return o;
        }

        try {
            LoginSession ent = loginSessionService.getInfo(vo.getAccess_token());
            if (ent == null) {
                o.setErrcode(9999);
                o.setErrmsg("access_token已失效!");
                return o;
            }
            o = orderService.receivingConfirmationBySelf(vo);

        } catch (Exception e) {
            logger.error(e);
            o.setErrcode(9999);
            o.setErrmsg("客户确认收货失败!");
        }
        return o;
    }

    @RequestMapping(value = "receivingConfirmationSpeBySelf", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "客户确认特产商品收货", httpMethod = "POST", notes = "客户确认特产商品收货", response = ResultObj.class)
    public @ResponseBody ResultObj receivingConfirmationSpeBySelf(@RequestBody ReceivingOrderSpeVO vo) {
        ResultObj o = new ResultObj();

        logger.info("receivingConfirmationSpeBySelf=>" + JsonUtils.toJson(vo));
        if (vo == null || StringUtils.isBlank(vo.getAccess_token())) {
            o.setErrcode(9999);
            o.setErrmsg("客户确认收货失败!access_token为空");
            return o;
        }

        try {
            LoginSession ent = loginSessionService.getInfo(vo.getAccess_token());
            if (ent == null) {
                o.setErrcode(9999);
                o.setErrmsg("access_token已失效!");
                return o;
            }

            o = orderService.receivingConfirmationSpeBySelf(vo);

        } catch (Exception e) {
            logger.error(e);
            o.setErrcode(9999);
            o.setErrmsg("客户确认收货失败!");
        }
        return o;
    }

    @RequestMapping(value = "querybybus", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询店铺订单信息", httpMethod = "POST", notes = "查询店铺自己订单信息 1:待处理 2:已完成 3:处理中", response = QueryOrderInfoRetVO.class)
    public @ResponseBody QueryOrderInfoRetVO querybybus(@RequestBody QueryOrderVO vo) {
        QueryOrderInfoRetVO o = new QueryOrderInfoRetVO();

        logger.info("querybybus=>" + JsonUtils.toJson(vo));
        if (vo == null || StringUtils.isBlank(vo.getAccess_token())) {
            o.setErrcode(9999);
            o.setErrmsg("查询店铺订单信息失败!access_token为空");
            return o;
        }

        try {
            LoginSession ent = loginSessionService.getInfo(vo.getAccess_token());
            if (ent == null) {
                o.setErrcode(9999);
                o.setErrmsg("access_token已失效!");
                return o;
            }

            o = orderService.querybybus(vo);
            o.setErrcode(0);
            o.setErrmsg("查询店铺订单信息成功!");
        } catch (Exception e) {
            logger.error(e);
            o.setErrcode(9999);
            o.setErrmsg("查询店铺订单信息失败!");
        }
        return o;
    }

    @RequestMapping(value = "detailsbybus", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询商家自己某些订单明细", httpMethod = "POST", notes = "查询商家自己的某些订单，返回优惠信息，且已计算完毕", response = ResultObj.class)
    public @ResponseBody DetailOrderRetVO detailsbybus(@RequestBody DetailOrderVO vo) {
        DetailOrderRetVO o = new DetailOrderRetVO();
        logger.info("detailsbybus=>" + JsonUtils.toJson(vo));

        if (vo == null || StringUtils.isBlank(vo.getAccess_token())) {
            o.setErrcode(9999);
            o.setErrmsg("查询商家自己某些订单明细失败!access_token为空");
            return o;
        }

        try {
            LoginSession ent = loginSessionService.getInfo(vo.getAccess_token());
            if (ent == null) {
                o.setErrcode(9999);
                o.setErrmsg("查询商家自己某些订单明细access_token已失效!");
                return o;
            }

            o.setErrcode(0);
            o.setErrmsg("查询商家自己某些订单明细信息成功!");
            o.setInfos(orderService.detailsbybus(vo));
        } catch (Exception e) {
            logger.error(e);
            o.setErrcode(9999);
            o.setErrmsg("查询商家自己某些订单明细信息失败!");
        }
        return o;
    }

    @RequestMapping(value = "confirm", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "商家确认订单信息", httpMethod = "POST", notes = "商家确认订单信息", response = ResultObj.class)
    public @ResponseBody ResultObj confirm(@RequestBody ConfirmOrderVO vo) {
        QueryOrderInfoRetVO o = new QueryOrderInfoRetVO();
        logger.info("confirm=>" + JsonUtils.toJson(vo));

        if (vo == null || StringUtils.isBlank(vo.getAccess_token())) {
            o.setErrcode(9999);
            o.setErrmsg("商家确认订单信息失败!access_token为空");
            return o;
        }

        try {
            LoginSession ent = loginSessionService.getInfo(vo.getAccess_token());
            if (ent == null) {
                o.setErrcode(9999);
                o.setErrmsg("access_token已失效!");
                return o;
            }

            orderService.confirmorder(vo);
            o.setErrcode(0);
            o.setErrmsg("商家确认订单信息成功!");
        } catch (Exception e) {
            logger.error(e);
            o.setErrcode(9999);
            o.setErrmsg("商家确认订单信息失败!");
        }
        return o;
    }

    @RequestMapping(value = "jobreceivingConfirmationSpeBySelf", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "客户端定时确认收货", httpMethod = "POST", notes = "客户端定时确认收货", response = ResultObj.class)
    public @ResponseBody ResultObj jobreceivingConfirmationSpeBySelf(@RequestBody JobReceivingOrderVO vo) {
        ResultObj o = new ResultObj();
        logger.info("jobreceivingConfirmationSpeBySelf=>" + JsonUtils.toJson(vo));

        try {
            orderService.jobreceivingConfirmationSpeBySelf(vo);
            o.setErrcode(0);
            o.setErrmsg("客户端定时确认收货!");
        } catch (Exception e) {
            logger.error(e);
            o.setErrcode(9999);
            o.setErrmsg("客户端定时确认收货失败!");
        }
        return o;
    }
}
